<!--
 * @Author: zhoulei0232
 * @Date: 2023-01-10 22:30:49
 * @LastEditTime: 2023-01-12 01:09:04
 * @LastEditors: zhoulei0232
 * @Description: 
-->
<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:banner:add']"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pageList">
      <el-table-column label="序号" align="center" type="index" width="100" />
      <el-table-column label="图片" align="center" prop="path">
        <template #default="scope">
          <el-image
            style="width: 200px; height: 50px"
            :src="uploadShowUrl + scope.row.path"
            :preview-teleported="true"
            :preview-src-list="[uploadShowUrl + scope.row.path]"
            :initial-index="0"
            fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="广告图名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />

      <el-table-column label="状态" align="center" prop="status" width="100">
        <template #default="scope">
          <dict-tag :options="stutasList" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="状态控制" align="center" prop="status">
        <template #default="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="2"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:banner:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="noticeRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="广告图名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入广告图名称" />
            </el-form-item>
            <el-form-item label="图片上传" prop="path">
              <el-upload
                class="avatar-uploader"
                :action="uploadUrl"
                :show-file-list="false"
                :before-upload="beforeAvatarUpload"
                :on-success="onSuccessImg"
              >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Platform">
import { listOrder } from "@/api/business/order";
import {
  listPlatform,
  addPlatform,
  updatePlatform,
} from "@/api/business/platform";
import { listBanner, addBanner, updateBanner } from "@/api/business/banner";
// 图片上传地址
const uploadUrl = import.meta.env.VITE_APP_UPLOAD_API + "api/uploadCos/banner";
const uploadShowUrl = import.meta.env.VITE_APP_COS_API;
const imageUrl = ref("");

const { proxy } = getCurrentInstance();

const fileList = ref([]); // 图片列表
const dialogImageUrl = ref(""); // 预览图url
const dialogVisible = ref(false); // 预览弹窗
const hideUpload = ref(false); // 是否隐藏上传按钮

const pageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);

const total = ref(0);
const title = ref("");

const openSub = ref(false); // 查看购买详情
const loadingSub = ref(false);
const pageListSub = ref([]);

const data = reactive({
  stutasList: [
    {
      label: "停用",
      value: "2",
      elTagClass: "",
      elTagType: "danger",
    },
    {
      label: "启用",
      value: "1",
      elTagClass: "",
      elTagType: "success",
    },
  ],
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    queryGoodsName: undefined,
    queryPainterName: undefined,
  },
  rules: {
    name: [{ required: true, message: "商品名称不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules, stutasList } = toRefs(data);

const beforeAvatarUpload = (rawFile) => {
  if (rawFile.type !== "image/png" && rawFile.type !== "image/jpeg") {
    ElMessage.error("Avatar picture must be PNG format!");
    return false;
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error("Avatar picture size can not exceed 2MB!");
    return false;
  }
  return true;
};

function onSuccessImg(response) {
  if (response.code == 200) {
    form.value.path = response.data;
    imageUrl.value = uploadShowUrl + response.data;
  }
}

/** 查询公告列表 */
function getList() {
  loading.value = true;
  listBanner(queryParams.value).then((response) => {
    pageList.value = response.data;
    total.value = 0;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    name: undefined,
    path: undefined,
  };
  imageUrl.value = null;
  proxy.resetForm("noticeRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增";
}
/**修改按钮操作 */
function handleUpdate(row) {
  reset();

  form.value = {
    id: row.id,
    name: row.name,
    path: row.path,
  };
  imageUrl.value = uploadShowUrl + row.path;
  open.value = true;
  title.value = "修改";
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["noticeRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateBanner(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBanner(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 用户状态修改  */
function handleStatusChange(row) {
  let text = row.status ? "启用" : "停用";

  proxy.$modal
    .confirm("确认要" + text + row.name + "吗?")
    .then(function () {
      let sendData = {
        id: row.id,
        status: row.status,
      };
      return updatePlatform(sendData);
    })
    .then(() => {
      proxy.$modal.msgSuccess(text + "成功");
    })
    .catch(function () {
      row.status = row.status == 1 ? 2 : 1;
    });
}

getList();
</script>
<style lang="scss" scoped>
.color-blue {
  color: rgb(0, 119, 255);
  cursor: pointer;
}
.avatar-uploader {
  width: 400px;
  height: 100px;
  .avatar {
    width: 100%;
    height: 100%;
  }
}
.avatar-uploader :deep(.el-upload) {
  width: 100%;
  height: 100%;
  border: 1px dashed #dcdfe6;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader :deep(.el-upload:hover) {
  border-color: #409eff;
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
